<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <!-- 搜索栏 -->
    <el-card class="search-card">
      <el-form
        ref="form"
        label-width="80px"
        style="width: 100%"
      >
        <el-row :gutter="60">
          <el-col :span="8">
            <el-form-item label="员工账号:">
              <el-input
                v-model="worklist.employeeNumber"
                style="width: 100%"
                placeholder="请输入员工账号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="员工姓名:">
              <el-input
                v-model="worklist.name"
                style="width: 100%"
                clearable
                placeholder="请输入员工姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工作模式:">
              <el-select
                v-model="worklist.workPatternId"
                placeholder="请选择工作模式"
                clearable
              >
                <el-option
                  v-for="(item, index) in patternList"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="月份:">
              <el-date-picker
                v-model="worklist.month"
                type="month"
                placeholder="请选择月份"
                clearable
                value-format="yyyy-MM"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属机构:">
              <treeselect
                v-model="worklist.agencyId"
                :options="treeList"
                :normalizer="normalizer"
                :searchable="true"
                style="width: 100%"
                placeholder="请选择所属机构"
                no-options-text="暂无数据"
                no-results-text="暂无数据"
                @select="selectAgency"
                @open="open"
                @close="close"
              />
              <img
                ref="arrow"
                src="../../../assets/pullDown.png"
                style="
                  position: absolute;
                  width: 18px;
                  height: 18px;
                  right: 9px;
                  top: 12px;
                  cursor: pointer;
                  pointer-events: none;
                  transition-duration: 0.3s;
                "
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button
              type="primary"
              @click="searchlist"
            >搜索
            </el-button>
            <el-button
              plain
              class="reset-btn"
              @click="restart"
            >重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 列表栏 -->
    <el-card
      class="list-card"
      style="padding: 10px 10px 30px 10px"
    >
      <div class="button-group">
        <el-button
          type="danger"
          @click="setScheduling"
        >排班设置
        </el-button>
        <el-button
          type="danger"
          plain
          @click="binding"
        >绑定排班
        </el-button>
      </div>
      <!-- 渲染列表 -->
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%; margin-top: 10px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          fixed
        ></el-table-column>
        <el-table-column
          prop="employeeNumber"
          label="员工账号"
          width="120"
          fixed
          header-align="center"
          align="center"
        />
        <el-table-column
          prop="name"
          label="员工姓名"
          width="120"
          fixed
          header-align="center"
          align="center"
        />
        <el-table-column
          prop="phone"
          label="员工电话"
          width="120"
          header-align="center"
          align="center"
          fixed
        />
        <el-table-column
          prop="userType"
          label="员工角色"
          width="120"
          fixed
          header-align="center"
          align="center"
        >
          <template #default="scope">
            {{ workCom(scope.row.userType) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="workPatternName"
          label="当前工作模式"
          width="120"
          fixed
          header-align="center"
          align="center"
        />
        <el-table-column
          v-for="(item, index) in tableData.length
            ? tableData[0].workSchedules
            : 30"
          :key="index"
          :label="(index + 1).toString()"
          width="50"
        >
          <template #default="{ row }">
            <div
              :class="{
                work: row.workSchedules[index],
                rest: !row.workSchedules[index]
              }"
            >
              {{ row.workSchedules[index] ? '上' : '休' }}
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        :current-page="worklist.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="worklist.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="display: flex; justify-content: center"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <!-- 绑定排班弹框 -->
    <el-dialog
      title="人员调整"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      class="my-dialog"
    >
      <span>
        <el-form
          ref="form"
          :model="relevance"
          label-width="80px"
        >
          <el-form-item>
            <el-radio-group v-model="relevance.workPatternType">
              <el-radio :label="1">礼拜制</el-radio>
              <el-radio :label="2">连续制</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="模式名称">
            <el-select
              v-show="relevance.workPatternType === 1"
              v-model="relevance.workPatternId"
              placeholder="请选择活动区域"
            >
              <el-option
                v-for="item in oneworklist"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
            <el-select
              v-show="relevance.workPatternType === 2"
              v-model="relevance.workPatternId"
              placeholder="请选择活动区域"
            >
              <el-option
                v-for="item in twoworklist"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </span>
      <span
        slot="footer"
        class="dialog-footer"
        style="margin: auto"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="sumbit"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { selectWorkHistoryList, workManage, peopleSet } from '@/api/transit'
import { agencyList } from '@/api/institutions'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: {
    Treeselect
  },
  data() {
    return {
      worklist: {
        agencyId: null, // 网点
        employeeNumber: '', // 员工账号
        month: '', // 月份
        name: '', // 员工名称
        page: 1, // 页数
        pageSize: 10, // 每页条数
        userType: '', // 用户类型
        workPatternId: '' // 工作模式
      },
      patternList: [], // 工作模式列表
      treeList: [], // 树状图数据
      defaultProps: {
        label: 'name'
      },
      // 树状图使用的数据
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      tableData: [],
      total: 10,
      dialogVisible: false,
      // 关联数据
      relevance: {
        userIdList: [],
        workPatternId: '',
        workPatternType: 1
      },
      workdialog: [],
      oneworklist: [],
      twoworklist: []
    }
  },
  created() {
    this.getPattern() // 获取工作模式列表数据
    this.getTreeList()
    this.getlabelList()
  },
  methods: {
    // 获取工作模式的下拉别表数据
    async getPattern() {
      const { data } = await selectWorkHistoryList()
      // console.log(...data[1], ...data[2])
      this.oneworklist = data[1]
      this.twoworklist = data[2]
      this.patternList = [...data[1], ...data[2]]
      // console.log(data, '6666')
      console.log(this.oneworklist, this.twoworklist, '66666')
    },
    // 获取树状数据
    async getTreeList() {
      const res = await agencyList()
      // console.log(res.data)
      this.treeList = JSON.parse(res.data)
      console.log(this.treeList)
    },
    // 赋予搜索id为搜索框内的数据
    selectAgency(val) {
      this.worklist.agencyId = val.id
    },
    // 打开所属机构下拉框
    open() {
      this.$refs.arrow.style.transform = 'rotate(-180deg)'
    },
    // 关闭所属机构下拉框
    close() {
      this.$refs.arrow.style.transform = 'rotate(0deg)'
    },
    // 选择框触发的事件
    handleSelectionChange(val) {
      this.relevance.userIdList = val.map(item => item.id)
    },
    // 获取列表数据
    async getlabelList() {
      const res = await workManage(this.worklist)
      // console.log(res, '666688')
      this.tableData = res.data.items
      // console.log(this.tableData, '6666')
      this.total = +res.data.counts
    },
    // 计算用户类型
    workCom(val) {
      if (val === 1) {
        return '员工'
      } else if (val === 2) {
        return '快递员'
      } else {
        return '司机'
      }
    },
    // 页面大小改变触发的事件
    handleSizeChange(val) {
      this.worklist.pageSize = val
      this.getlabelList()
    },
    // 页面跳转触发的事件
    handleCurrentChange(val) {
      this.worklist.page = val
      this.getlabelList()
    },
    // 搜索功能
    searchlist() {
      this.getlabelList()
    },
    // 重置功能
    restart() {
      this.worklist = {
        agencyId: null, // 网点
        employeeNumber: '', // 员工账号
        month: '', // 月份
        name: '', // 员工名称
        page: 1, // 页数
        pageSize: 10, // 每页条数
        userType: '', // 用户类型
        workPatternId: '' // 工作模式
      }
      this.getlabelList()
    },
    // 绑定弹框打开
    binding() {
      if (this.relevance.userIdList.length > 0) {
        this.dialogVisible = true
      } else {
        this.$message.error('请选择调整人员')
      }
    },
    // 关闭弹框
    handleClose() {
      this.dialogVisible = false
    },
    // 点击确认按钮
    async sumbit() {
      if (!this.relevance.workPatternId) {
        this.$message.error('请选择工作模式')
        return
      }
      try {
        await peopleSet(this.relevance)
        this.$message.success('调整成功')
        this.getPattern()
        this.handleClose()
      } catch (error) {
        this.$message.error('调整失败')
      }
    },
    setScheduling() {
      this.$router.push('/transit/workArrange-setting')
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep th.date {
  .cell {
    padding-left: 17px !important;
  }
}

.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}

.history-name {
  width: auto !important;

  ::v-deep .el-input__inner {
    width: 293px;
  }
}

.workArrange-manage-people-setting {
  ::v-deep .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }

  ::v-deep .el-dialog__footer {
    // text-align: center;
    padding-bottom: 40px;
  }
}

.work-type {
  ::v-deep .el-form-item__content {
    margin-left: 40px !important;
  }

  ::v-deep .el-radio:first-child {
    margin-right: 80px !important;
  }

  ::v-deep .el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.alert {
  margin: 10px 0px;
}

.pagination {
  margin-top: 40px;
}

.transport-task ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  // overflow: hidden;
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;

  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}

::v-deep .el-card {
  overflow: initial;
}
</style>

<style lang="scss" scoped>
.search-card {
  margin: 20px 20px;
}

.list-card {
  margin: 10px 20px;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}

.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.pagination-container ul {
  text-align: center;
}

::v-deep .el-dialog {
  .el-dialog__footer {
    text-align: center;
  }
}

::v-deep .el-dialog {
  .el-form {
    padding: 0 80px;

    .radio-group {
      margin-bottom: 30px;
      // padding-left: 10px;
    }
  }
}
</style>
